package com.dykj.fireFighting.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dykj.fireFighting.api.base.file.entity.DataFiles;
import com.dykj.fireFighting.api.base.file.service.DataFilesService;
import com.dykj.fireFighting.web.mapper.DataFilesMapper;
import com.dykj.fireFighting.web.mongodb.GridFSRepository;
import com.dykj.fireFighting.web.utils.FileUploadUtil;
import com.mongodb.client.gridfs.model.GridFSFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 资料库
 *
 * @Author JacksonTu
 * @Date  2021-06-19 09:13:50
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class DataFilesServiceImpl  extends ServiceImpl<DataFilesMapper, DataFiles> implements DataFilesService {


    @Value("${file.access-path-pattern}")
    private String accessPathPattern = "/u/**";

    @Value("${file.upload-folder}")
    private String uploadFolder;

    @Value("${file.access-prefix-url}")
    private String accessPrefixUrl;


    @Autowired
    private GridFSRepository gridFSRepository;

    @Autowired
    private GridFsTemplate gridFsTemplate;


    @Override
    public String uploadFile(MultipartFile file) {
        DataFiles dataFiles=gridFSRepository.saveFile(file);
        if(dataFiles!=null)
        {
            this.baseMapper.insert(dataFiles);
            return dataFiles.getId();
        }
        return null;
    }
    @Override
    public InputStream getFile(String id) {
        GridFSFile  gridFSFile=gridFSRepository.findOne(id);
        if(gridFSFile!=null)
        {
            GridFsResource resource = gridFsTemplate.getResource(gridFSFile);
           return resource.getContent();
        }
        return null;
    }

    @Override
    public int updateFile(List<String> list, String reId) {
        int size=0;
        if(list.size()>0)
        {
            size=this.baseMapper.updateFile(list,reId);
        }
        return size;
    }

    @Override
    public DataFiles getByMongoId(String mongoId) {
        LambdaQueryWrapper<DataFiles> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq( DataFiles::getFileMongoId, mongoId);
        wrapper.eq(DataFiles::getIsDel,0);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public String uploadFile(ByteArrayInputStream ins, int length, String fileName, String contentType) {
        DataFiles dataFiles = gridFSRepository.saveFile(ins, fileName, length, contentType);
        if (dataFiles != null) {
            this.baseMapper.insert(dataFiles);
            return dataFiles.getId();
        }
        return null;
    }

    @Override
    public List<DataFiles> queryReId(String reId) {
        LambdaQueryWrapper<DataFiles> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataFiles::getReId, reId);
        wrapper.eq(DataFiles::getIsDel, 0);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public byte[] getDataByFileId(String fileId) throws Exception {
        DataFiles dataFiles = getById(fileId);
        InputStream inputStream = getFile(dataFiles.getFileMongoId());

        byte[] content = IOUtils.toByteArray(inputStream);
        return content;
    }

    @Override
    public List<DataFiles> getDataFilesByIds(List<String> ids) {
        return this.baseMapper.getDataFilesByIds(ids);
    }

    @Override
    public String uploadLocal(MultipartFile file,String enterpriseName) {
        if (file == null || file.isEmpty()) {
            return "文件不能为空";
        }
        String relativePath = FileUploadUtil.uploadLocal(file, uploadFolder + "/"+ enterpriseName);

        if (!StringUtils.endsWith(accessPrefixUrl, "/")) {
            accessPrefixUrl += '/';
        }
        return relativePath;
    }

}
